package junit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mysql.jdbc.EscapeTokenizer;

import cn.pdx.elec.container.ServiceProvider;
import cn.pdx.elec.dao.ElecTextDao;
import cn.pdx.elec.domain.ElecText;

public class TestDao {
   
	/**
	 * 保存
	 */
	@Test
	public void seveElecText(){
		
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//返回一个接口
		ElecTextDao elecTextDao=(ElecTextDao) applicationContext.getBean(ElecTextDao.SERVICE_NAME);
		
		//实例化对象，并保存
		ElecText elecText=new ElecText();
		
		elecText.setTextName("测试dao");
		elecText.setTextDate(new Date());
		elecText.setTextRemark("测试dao备注");
		elecTextDao.save(elecText);
	}

	/**
	 * 修改
	 */
	@Test
	public void updateElecText(){
		
		
		//调用方法返回一个服务接口
		ElecTextDao elecTextDao=(ElecTextDao) ServiceProvider.getService(ElecTextDao.SERVICE_NAME);
		
		//实例化对象，并保存
		ElecText elecText=new ElecText();
		elecText.setTextID("40288dfa3d9f329b013d9f32ab5b0001");
		elecText.setTextName("张柏芝");
		elecText.setTextDate(new Date());
		elecText.setTextRemark("测试她");
		elecTextDao.update(elecText);
	}
	
	/**
	 * 通过Id查询对象
	 */
	@Test
	public void findObjectById(){
		
		//调用方法返回一个服务接口
		ElecTextDao elecTextDao=(ElecTextDao) ServiceProvider.getService(ElecTextDao.SERVICE_NAME);
		
		Serializable id="40288dfa3d9f329b013d9f32ab5b0001";
		
		//这里调用泛型转换，转换为要查找的那个对象
		ElecText elecText=elecTextDao.findObjectById(id);
		
		System.out.println(elecText.getTextName()+"  "+elecText.getTextRemark());
		
	}
	
	/**
	 * 通过Ids数组删除对象
	 */
	@Test
	public void deleteObjectByIds(){
		
		//调用方法返回一个服务接口
		ElecTextDao elecTextDao=(ElecTextDao) ServiceProvider.getService(ElecTextDao.SERVICE_NAME);
		
		Serializable[] ids={"40288dfa3d9b2f0b013d9b2f11130001","40288dfa3d9b355c013d9b3561f40001"};
		
		//调用删除方法
		elecTextDao.deleteObjectByIds(ids);
		
	}
	
	/**
	 * 通过集合对象进行删除
	 */
	@Test
	public void deleteObjectByCollection(){
		
		//调用方法返回一个服务接口
		ElecTextDao elecTextDao=(ElecTextDao) ServiceProvider.getService(ElecTextDao.SERVICE_NAME);
		
		List<ElecText> list=new ArrayList<ElecText>();
		
		//创建对象1
		ElecText elecText1=new ElecText();
		elecText1.setTextID("40288dfa3d9b86d3013d9b86dd540001");
		
		//创建对象2
		ElecText elecText2=new ElecText();
		elecText2.setTextID("40288dfa3da0d764013da0d768280001");
		
		//添加进list
		list.add(elecText1);
		list.add(elecText2);
		//调用删除方法
		elecTextDao.deleteObjectByCollection(list);
	}
}
